#include "message.h"

Message::Message()
{ 
	Data = NULL;
	instCount = new int(0);
	MsgCode = MSG_NULL;
}

Message::Message(const string& f, const string& t, int c, CData* d)
	: From(f), To(t), MsgCode(c), Data(d)
{
	instCount = new int(1);
}

Message::Message(const Message& copy)
{
	From = copy.From;
	To = copy.To;
	MsgCode = copy.MsgCode;
	Data = copy.Data;
	instCount = copy.instCount;
	(*instCount)++;
}

Message& Message::operator=(const Message& copy)
{
	if( (*instCount) <= 1)
	{
		delete Data;
		delete instCount;
	}else 
	if( (*instCount) >1 )
	{
		(*instCount)--;
	}

	From = copy.From;
	To = copy.To;
	MsgCode = copy.MsgCode;
	Data = copy.Data;
	instCount = copy.instCount;
	(*instCount)++;
	return (*this);
}

Message::~Message() 
{ 
	cout << (*instCount) << endl;
	if( (*instCount)<=1)
	{
		delete Data;
		delete instCount;
	}
	else
		(*instCount)--;
}

void Message::Set(const string& f, const string& t, int c, CData* d)
{
	if( (*instCount) == 0)
	{
		(*instCount)++;
	}
	else if( (*instCount) == 1)
	{
		delete Data;
	}
	else if( (*instCount) >1 )
	{
		(*instCount)--;
		instCount = new int(1);
	}

	From = f;
	To = t;
	MsgCode = c;
	Data = d;
}